package ru.chaykin.measures;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

import ru.chaykin.primitives.Point;


public abstract class Measure implements IMeasure {

	protected float radius;
	protected static float startSize = 0.02f; //Начальный размер кружка
	protected FloatBuffer linesBuff;
	protected List<FloatBuffer> marksBuffs = new ArrayList<FloatBuffer>();
	protected int size = 0;  //Число точек в полилинии
	
	protected void initMarksBuff() {
		ByteBuffer bb = ByteBuffer.allocateDirect(12 * ANGLES);
		bb.order(ByteOrder.nativeOrder());
		marksBuffs.add(bb.asFloatBuffer());
	}

	protected void buildCircle(int index, Point p, float radius) {
		if (index < marksBuffs.size()) {
			marksBuffs.get(index).position(0);
			for (int i = 0; i < ANGLES; i++) {
				marksBuffs.get(index).put(p.getx() + (float) (radius * Math.sin(i * 2 * Math.PI / ANGLES)));
				marksBuffs.get(index).put(p.gety() + (float) (radius * Math.cos(i * 2 * Math.PI / ANGLES)));
				marksBuffs.get(index).put(Point.z);
			}
			marksBuffs.get(index).position(0);
		}
	}

	public void zoom(float m) {
		radius = startSize * m;
		for (int i = 0; i < 3 * size; i += 3) {
			buildCircle(i / 3, new Point(linesBuff.get(i), linesBuff.get(i + 1)), radius);
		}
	}
	
	public static String getMeasuredData(IMeasure measure) {
		if (measure != null)  
			return measure.getMeasuredData();
		return "";
	}
}
